package com.tian.netty.executor;

import com.alibaba.fastjson.JSON;
import com.tian.dto.ChargeStationGunStatusInfo;
import com.tian.entity.ChargeStationGun;
import com.tian.enums.ChargerStatusEnum;
import com.tian.mapper.ChargeStationGunMapper;
import com.tian.message.ChargingDataMessage;
import com.tian.message.MessageReqIdPrefixConstant;
import com.tian.message.StationGunCommunicateLogMessage;
import com.tian.netty.message.resp.ChargerStartResp;
import com.tian.netty.test.charging.ChargingDataDto;
import com.tian.netty.message.MsgType;
import com.tian.netty.message.req.ChargingDataReq;
import com.tian.producer.ChargingRecordProducer;
import com.tian.util.DateUtils;
import com.tian.util.StationRedisPreKey;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;
//import plus.ojbk.influxdb.core.InfluxdbTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * {@code @description:} 充电数据实时上报
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-05-14 1:27
 * {@code @version:} 1.0
 */
@Slf4j
@Component(MsgType.MSG_TYPE_PRE + "64")
public class ChargingDataExecutor extends RequestExecutorBase {
    @Resource
    private RedissonClient redissonClient;
    @Resource
    private ChargingRecordProducer chargingRecordProducer;

    @Override
    public void run() {
        ChargingDataReq chargingDataReq = new ChargingDataReq(msgObject[1], msgObject);
        ChargingDataDto chargingDataDto;
        try {
            chargingDataDto = chargingDataReq.parse();
        } catch (Exception e) {
            log.error("解析充电数据失败{}", e.getMessage());
            return;
        }

        // 发送消息到MQ中
        ChargingDataMessage chargingDataMessage = new ChargingDataMessage();
        chargingDataMessage.setGunNo(chargingDataDto.getGunNo());
        chargingDataMessage.setUserId(chargingDataDto.getUserId());
        chargingDataMessage.setChargingCount(chargingDataDto.getChargingCount());
        chargingDataMessage.setChargingEndTime(chargingDataDto.getChargingEndTime());
        chargingDataMessage.setChargingStartTime(chargingDataDto.getChargingStartTime());
        chargingDataMessage.setRemainedChargingTime(chargingDataDto.getRemainedChargingTime());
        chargingDataMessage.setReqId(MessageReqIdPrefixConstant.CHARGING_DATA_REQ_ID_PREFIX
                + UUID.randomUUID() + DateUtils.formatDefaultDateMs());
        chargingRecordProducer.send(chargingDataMessage);
        log.info("充电数据上报成功{}", chargingDataMessage);
        ChargerStartResp response = new ChargerStartResp(MsgType._0X_41);
        Map<String, Object> respBody = new HashMap<>();
        respBody.put("gunNo", chargingDataDto.getGunNo());
        response.setResponseBody(respBody);
        response.format();
        channel.writeAndFlush(response.getMsgObject());
    }
}
